package entity;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.StrBuilder;
import cn.hutool.core.util.ObjectUtil;
import com.google.common.collect.Lists;
import com.google.errorprone.annotations.Var;
import org.apache.commons.lang.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author youdao.hww
 * @date 2023-12-07
 * test
 */
public class Test2 {

    public static void main(String[] args) {

        LargeLiquidReceiveSummaryDTO one = new LargeLiquidReceiveSummaryDTO();
        one.setIdlist("101620681");
        one.setMedicationUnitType(3);
        one.setMedicationUnit("瓶");
        one.setMedicationNum(BigDecimal.valueOf(1));
        one.setReductionNum(BigDecimal.valueOf(1));
        one.setRealProviderNum(BigDecimal.valueOf(1));
        one.setSmallLargePackageRatio(1);
        one.setSource(1);
        one.setMedicationId(1L);
        one.setExecutiveDeptId(1074L);
        one.setExecutiveWardareaId(1L);

        LargeLiquidReceiveSummaryDTO two = new LargeLiquidReceiveSummaryDTO();
        two.setIdlist("101620682");
        two.setMedicationUnitType(4);
        two.setMedicationUnit("瓶");
        two.setMedicationNum(BigDecimal.valueOf(2));
        two.setReductionNum(BigDecimal.valueOf(1));
        two.setRealProviderNum(BigDecimal.valueOf(2));
        two.setSmallLargePackageRatio(1);
        two.setSource(1);
        two.setMedicationId(1L);
        two.setExecutiveDeptId(1074L);
        two.setExecutiveWardareaId(1L);

        List<LargeLiquidReceiveSummaryDTO> summarylist = Arrays.asList(one, two);

        summarylist = summarylist.stream().collect(ArrayList::new, (a, b) -> {
            if (StringUtils.isNotBlank(b.getIdlist())) {
                List<Long> list = Lists.newArrayList();
                String[] ids = b.getIdlist().split(",");
                for (String id : ids) {
                    list.add(Long.parseLong(id));
                }
                b.setIds(list);
            }
            a.add(b);
        }, ArrayList::addAll);


        List<LargeLiquidReceiveSummaryDTO> largeLiquidReceiveSummaryDTOS = setLargeUnit(summarylist);
        System.out.println("largeLiquidReceiveSummaryDTOS = " + largeLiquidReceiveSummaryDTOS);
    }

    private static List<LargeLiquidReceiveSummaryDTO> setLargeUnit(List<LargeLiquidReceiveSummaryDTO> medicationSum) {
        if (CollUtil.isEmpty(medicationSum)) {
            return Lists.newArrayList();
        }
        //根据病区 来源  药品ID分组
        Map<String, List<LargeLiquidReceiveSummaryDTO>> listMap = medicationSum.stream()
                .collect(Collectors.groupingBy(v ->
                        StrBuilder.create().append(v.getExecutiveWardareaId())
                                .append("_").append(v.getSource())
                                .append("_").append(v.getMedicationId())
                                .append("_").append(v.getExecutiveDeptId()).toString()));
        List<LargeLiquidReceiveSummaryDTO> result = Lists.newArrayList();
        listMap.forEach((k, v) -> {
            //如果比值为1 合并
            if (ObjectUtil.isNotNull(v.get(0).getSmallLargePackageRatio()) && v.get(0).getSmallLargePackageRatio().equals(1)) {
                BigDecimal medicationNum = BigDecimal.ZERO;
                BigDecimal reductionNum = BigDecimal.ZERO;
                BigDecimal realProviderNum = BigDecimal.ZERO;
                List<Long> ids = Lists.newArrayList();
                for (LargeLiquidReceiveSummaryDTO vv : v) {
                    medicationNum = vv.getMedicationNum().add(medicationNum);
                    reductionNum = vv.getReductionNum().add(reductionNum);
                    realProviderNum = vv.getRealProviderNum().add(realProviderNum);
                    ids.addAll(vv.getIds());
                }
                LargeLiquidReceiveSummaryDTO b = v.get(0);
                b.setIds(ids);
                b.setMedicationNum(medicationNum);
                b.setReductionNum(reductionNum);
                b.setRealProviderNum(realProviderNum);
                b.setMedicationUnitType(4);
                // 合并后 设置大单位
                b.setMedicationUnit(b.getLargePackagingUnit());
                result.add(b);
            } else {
                result.addAll(v);
            }
        });
        return result;
    }

}
